package com.icart.controller.personController;

import com.icart.context.HttpUserContext;

import com.icart.context.storage.ContextStorage;
import com.icart.context.storage.SessionContextStorageImpl;

import com.icart.controller.WxUtilController;
import com.icart.repositories.CustinfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/person")
public class PersonHomeController {

    @Autowired
    private CustinfoRepository custinfoRepository;
    @Autowired
    HttpServletRequest request;
    @Autowired
    private RestTemplate template;

    //个人中心首页
    @RequestMapping("/personhome")
    public String person(Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");
        if(userContext!=null){
            /*获取用户名，在页面判断哪些该展示，哪些不展示*/
            model.addAttribute("custname",userContext.getCustid());
            model.addAttribute("custname2",userContext.getCustname());
            System.out.println("1111="+userContext.getCustname());

            /*查出车型展示*/
            String sql="select (select d.brandname from bi_carbrand d where d.brandid=f.brandid)brandname," +
                    "(select e.carlinename from bi_carline e where e.carlineid=f.carlineid)carlinename,f.carvol,f.caryear,r.regdate,r.curkilo " +
                    "from cu_custcar r,bi_cardef f where r.cartype=f.cartypeid and r.setdefault=1 and r.custid="+userContext.getCustid();
            List<Map> list =custinfoRepository.createNativeQuery(sql);
            if(list.size()>0){
                model.addAttribute("car",list.get(0));
            }else {
                model.addAttribute("car",null);
            }


            /*查出我的订单数*/
            String sql2="select count(*) as ordernum from wo_info where (wotype <>'sale' or wotype is null) and custid="+userContext.getCustid();
            List<Map> list2 =custinfoRepository.createNativeQuery(sql2);
            model.addAttribute("ordernum",list2.get(0));

            /*查看优惠券数*/
            Date date=new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String nowtime=sdf.format(date);
            String sql3="select count(*) as couponnum from cu_coupon where startdate<='"+nowtime+"'"+" and enddate>='"+nowtime+"' and stat=0";
            List list3 =custinfoRepository.createNativeQuery(sql3);
            model.addAttribute("couponnum",list3.get(0));
            String openid=request.getSession().getAttribute("openid").toString();
            WxUtilController wx=new WxUtilController();
            Object user=wx.getUser(template,request,openid);
            model.addAttribute("user",user);
        }
        return "person/personhome/personhome";
    }


}